﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace UofCASE.AgileProductLinerDSL {
    // Bounding   
    partial class FeaturePortShape {

        public override BoundsRules BoundsRules {
            get {
                return new FeaturePortBoundsRules();
            }
        }

        public override bool HasConnectionPoints {
            get {
                return true;
            }
        }

        public override void EnsureConnectionPoints(LinkShape link) {
            this.ConnectionPoints.Clear();
            this.CreateConnectionPoint(new PointD(AbsoluteBoundingBox.Center.X, AbsoluteBoundingBox.Top));
        }
    }

    public class FeaturePortBoundsRules : BoundsRules {

        public override RectangleD GetCompliantBounds(ShapeElement shape, RectangleD proposedBounds) {

            FeaturePortShape portShape = shape as FeaturePortShape;
            TestShape parentShape = portShape.ParentShape as TestShape;

            Diagram diagramm = portShape.Diagram;

            // on initial creation, there is no parent shape
            if (parentShape == null) {
                System.Diagnostics.Debug.WriteLine("No partent shape");
                return proposedBounds;
            }

            double countFeaturePorts = 1;
            int placeOfPort = 0;


            List<double> linkPos = new List<double>();

            Test test = parentShape.ModelElement as Test;
            if (test != null) {
                countFeaturePorts = test.FeaturePorts.Count + 1;
                foreach (FeaturePort fport in test.FeaturePorts) {
                    if (fport.TestPort != null) {
                        ShapeElement shapeElement = diagramm.FindShape(fport.TestPort.Feature);
                        if (shapeElement == null) {
                            System.Diagnostics.Debug.WriteLine("Shape not found in diagram");
                            return proposedBounds;
                        }
                        System.Diagnostics.Debug.WriteLine("Shape found in diagram");
                        linkPos.Add(shapeElement.AbsoluteBoundingBox.Center.X);
                    } else return proposedBounds;
                }
            }

            double x = 0;
            double y = 0;

            if (linkPos.Count > 1) {
                linkPos.Sort();
                FeaturePort featurePort = portShape.ModelElement as FeaturePort;
                ShapeElement thisFeatureShape = diagramm.FindShape(featurePort.TestPort.Feature);
                if (thisFeatureShape == null) {
                    System.Diagnostics.Debug.WriteLine("Shape not found in diagram, 2");
                    return proposedBounds;
                }
                placeOfPort = linkPos.IndexOf(thisFeatureShape.AbsoluteBoundingBox.Center.X) + 1;

                x = parentShape.Size.Width * (placeOfPort / countFeaturePorts);
                y = -proposedBounds.Height / 2;
            } else {
                x = parentShape.Size.Width / 2;
                y = -proposedBounds.Height / 2;
            }
            return new RectangleD(x, y, proposedBounds.Width, proposedBounds.Height);
        }
    }
}
